<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
    <script type="text/babel">
      /**
       *字符串
       */
      for (let codePoint of "fod") {
        console.log(codePoint);
      }

      // 模板字符串 ``
      // 模板字符串（template string）是增强版的字符串，用反引号（`）标识。它可以当作普通字符串使用，也可以用来定义多行字符串，或者在字符串中嵌入变量。
      // 普通字符串
      console.log(`这是一个普通字符串`);
      // 多行字符串   输出时空格和换行都被保留
      let a = `In Javascript this is 
        not legal
      `;
      console.log(a);
      // 字符串中嵌入变量
      let b = "张三";
      let c = `今天\`天气\`很好，${b}出去游玩了`;
      console.log(c);

      let d = 1;
      let e = 2;
      console.log(`${d}+${e}=${d + e}`);
      console.log(`${d}+${e * 2}=${d + e * 2}`);
      let obj = { x: 1, y: 2 };
      console.log(`${obj.x + obj.y}`);

      function fun() {
        return "你好";
      }
      console.log(`${fun()},我叫TOM`);

      let func = (name) => `hello,${name}`;
      console.log(func("a"));

      let f = 5;
      let g = 10;

      function tag(s, v1, v2) {
        console.log(s);
        console.log(s[0]);
        console.log(s[1]);
        console.log(s[2]);
        console.log(s[3]);
        console.log(v1);
        console.log(v2);
        return "over";
      }
      tag`hello ${f + g} world ${f * g}`;

      let total = 30;
      let msg = passthru`The total is ${total} (${total * 1.05} with tax)`;
      function passthru(literals) {
        console.log(literals);
        let result = "";
        let i = 0;
        while (i < literals.length) {
          result += literals[i++];
          console.log(result);
          console.log(arguments);
          if (i < arguments.length) {
            result += arguments[i];
          }
        }
        return result;
      }
      console.log(msg);

      /**
       * 字符串方法
       *
       */
      // String.fromCodePoint() 从 Unicode 码点返回对应字符
      console.log(String.fromCodePoint(0xDe20))
      // String.raw() 返回一个斜杠都被转义的字符串，常用与模板字符串的处理方法
      console.log(String.raw`Hi\n${2+3}`)
      console.log(String.raw`Hi\\n`)
      // codePointAt() 能够正确处理4个字节储存的字符，返回一个字符等到码点
      // 使用codePintAt() 方法测试一个字符由两个字节还是四个字节组成的最简单方法
      function is32Bit(c){
        return c.codePointAt(0)>0xFFFF
      }
      console.log(is32Bit("a"))
      // includes(),starstWith(),endsWith() 用来在一个字符串中找另一个字符串，返回值均为布尔值
      let s =" my name is tom "
      console.log(s.includes("i"))
      console.log(s.startsWith("y"))
      console.log(s.endsWith(" "))
      console.log(s.includes("i",9))
      console.log(s.startsWith("y",2)) 
      console.log(s.endsWith(" ",4))// 针对数值前的字符串

      //repeat()返回一个新字符串，表示将原来字符串重复几次
      console.log('x'.repeat(2))
      console.log("na".repeat(0))
      console.log("na".repeat(2.3))
      console.log("na".repeat(-0.6))
      console.log("na".repeat(NaN))

      console.log("na".repeat("na"))
      console.log("na".repeat("2"))
      // trimStart() trimEnd() 消除前，后空格
      let l = "  abc    "
      console.log(l)
      console.log(l.trim())
      console.log(l.trimStart())
      console.log(l.trimEnd())

    </script>
  </body>
</html>
